var base = require('/base_info.js');
var utils = require('/utils/index.js');
Page({
  // 选择图片
  data: {
    imgList: [],
    imgUrl: base.imgUrl,
    starNum: 0,
    content: ''
  },
  // 文本域改变 
  textContent (e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 选择图片  
  choseImg () {
    let that = this;
    my.chooseImage({
      count: 3,
      success: (res) => { // 成功选择图片的回调
        console.log(JSON.stringify(res));
        that.setData({
          imgList: res.apFilePaths
        })
        // 调用上传图片的api
        that.upImg();
      }
    });
  },
  // 选择星级
  choseStar (e) {
    this.setData({
      starNum: e.currentTarget.dataset.id * 1
    })
  },
  // 点击评论
  submitComment () {
    let that = this;
    if (that.data.content == "") {
      my.alert({
        content: '请输入评价内容' 
      });
      return false;
    };
    my.showLoading();
    new Promise((resolve, reject) => {
      my.textRiskIdentification({
        content: that.data.content,
        type: ['0', '1', '2', '3'],
        success: function (res) {
          for (var i = 0; i < res.result.length; i++){
            if (res.result[i].score * 1 > 90) {
              // 命中敏感字符
              reject(res.result[i].score);
              return false;
            }
          };
          resolve();
        },
        fail: (res) => {
          my.alert({
            content: '网络异常，请稍候再试' 
          });
        },
      });
    }).then((score) => {
      // 请求评论
      utils.getData('SingleSaveAssess', {
        order_sn: that.data.orderInfo.order_no,
        user_id: my.getStorageSync({
          key: 'uid', // 缓存数据的key
        }).data,
        star: that.data.starNum,
        content: that.data.content
      }, (res) => {
        my.hideLoading();
        if (res.data.errno == 1) {
          my.alert({
            content: `评价成功`,
            success: () => {
              my.navigateBack(); 
            }
          });
          return false;
        };
        my.alert({
          content: `${res.data.message}`,
        });
      }, (err) => {
        my.hideLoading();
        my.alert({
          content: `网络错误，请稍后再试`,
        });
      });
    }, (err) => {
      my.hideLoading();
      my.alert({
        content: `评价内容包含敏感字符，请稍候再试`,
      });
    })    
  },
  // 获取订单数据
  getData () {
    let that = this;
    my.showLoading();
    utils.getData('SingleOrderDetails', {
      order_id: that.data.order_id
    }, (res) => {
      my.hideLoading();
      that.setData({
        orderInfo: res.data.data
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 上传图片
  upImg () {
    let that = this;
    that.data.imgList.forEach((item, index) => {
      console.log(that.data.imgList[index]);
      my.uploadFile({
        url: base.baseurl,
        fileType: 'image',
        fileName: "upfile",
        formData: {
          i: base.i,
          c: base.c,
          a: base.a,
          m: base.m,
          d: "Upload",
          name: "upfile"
        },
        filePath: that.data.imgList[index],
        success: (res) => {  // 成功上传的回调
          console.log(res);
          my.alert({
            content: '上传成功'
          });
        },
        fail: (err) => {
          console.log(err);
        }
      });
    });
  },
  onLoad(query) {
    console.log(query);
    this.setData({
      order_id: query.id
    });
    this.getData();
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
